# -*- coding: utf-8 -*-
import pymysql

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html


class ScrapyhexunPipeline(object):
    def __init__(self):
        self.db = pymysql.connect(host="127.0.0.1",db="autopjt",user="root",passwd="root",charset="utf8")
        
    def process_item(self, item, spider):
        item = dict(item)
        sqlDatas = []
        
        for j in range(0,len(item["name"])):
            sqlDatas.append((item["name"][j],item["url"][j],item["hits"][j],item["comment"][j]))
        
        print("sqlDatas = ",sqlDatas)    
        if len(sqlDatas)>0:
            sql = "INSERT INTO hexun_blog(name,url,hits,comment)VALUES(%s,%s,%s,%s)"
            
            cur = self.db.cursor()
            try:
                cur.executemany(sql,sqlDatas)
                self.db.commit()
            except Exception as e:
                print("事务回滚，出现异常：",e)
                self.db.rollback()
                raise e
            finally:
                cur.close()
                return item
        else:
            return item
        
    def close_spider(self,spider):
        self.db.close()